package com.zrz.task;


import com.zrz.dao.auto.UserMapper;
import com.zrz.model.auto.Record;
import com.zrz.model.auto.User;
import com.zrz.model.auto.UserExample;
import com.zrz.service.UserService;
import com.zrz.service.WechatService;
import com.zrz.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class VipTaskService {
    @Autowired
    private  UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WechatService wechatService;

    /**
     * 会员过期 0 0 1 * * ?
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void overUser(){
        UserExample userExample=new UserExample();
        userExample.createCriteria().andSurplusCountLessThanOrEqualTo(0).andMemberEqualTo(true);
        List<User> userList=userMapper.selectByExample(userExample);
        userList.forEach(user -> {
            user.setMember(false);
            user.setTotalCount(0);
            user.setSurplusCount(0);
            userMapper.updateByPrimaryKey(user);
        });
    }
    /**
     * 会员酒未领取-1 0 59 23 L * ?
     */
//    @Scheduled(cron = "0 0 1 1 * ?")
//    public void notGet(){
//        UserExample userExample=new UserExample();
//        userExample.createCriteria().andMemberEqualTo(true).andSurplusCountGreaterThan(0);
//        List<User> userList=userMapper.selectByExample(userExample);
//        userList.forEach(user -> {
//            List<Record> recordList=userService.getRecord(user.getId());
//            if(user.getStartMonth()==2){
//                if(DateUtils.getDateBySimplDate(DateUtils.getDaySub(),"yyyy-MM").equals(DateUtils.getDateBySimplDate(user.getStartDate(),"yyyy-MM"))){
//                    return;
//                }
//            }
//            if(CollectionUtils.isEmpty(recordList)){
//                user.setSurplusCount(user.getSurplusCount()-1);
//            }else{
//                Record record=recordList.get(0);
//                if(!DateUtils.getDateBySimplDate(record.getGetTime(),"yyyy-MM").equals(DateUtils.getDateBySimplDate(DateUtils.getDaySub(),"yyyy-MM"))){
//                    user.setSurplusCount(user.getSurplusCount()-1);
//                }
//            }
//            userMapper.updateByPrimaryKey(user);
//        });
//    }

    /**
     * 会员过期 0 0 1 * * ?
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void image(){
        wechatService.getMediaid();
    }
}
